package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/model"
	"strconv"
	"time"
)

func PersonAdd(c *gin.Context) {
	person_name := c.PostForm("person_name")
	person_tel := c.PostForm("person_tel")
	if person_tel == "" || person_name == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	data := model.Person{
		PersonName: person_name,
		PersonTel:  person_tel,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func PersonOrderAdd(c *gin.Context) {
	person_code, _ := strconv.Atoi(c.PostForm("person_code"))
	person_price := c.PostForm("person_price")
	person_form := c.PostForm("person_form")
	person_to := c.PostForm("person_to")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	data := model.PersonOrder{
		PersonCode:  person_code,
		PersonPrice: person_price,
		PersonForm:  person_form,
		PersonTo:    person_to,
		Pid:         pid,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func PersonOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var data []model.PersonOrderList
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("person_orders").
			Select(
				"person_orders.id as OrderId",
				"people.person_name as PersonName",
				"people.person_tel as PersonTel",
				"person_orders.person_code as PersonCode",
				"person_orders.person_price AS PersonPrice",
				"person_orders.person_form AS PersonForm",
				"person_orders.person_to AS PersonTo",
			).Joins("LEFT JOIN people ON person_orders.pid=people.id").
			Offset(offset).Limit(size).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	var total int64
	model.DB.Model(&model.PersonOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  data,
		"total": total,
	})

}
func PersonOrderListFind(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	var data model.PersonOrderList
	redis_key := "list_id_" + strconv.Itoa(id)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("person_orders").
			Select(
				"person_orders.id as OrderId",
				"people.person_name as PersonName",
				"people.person_tel as PersonTel",
				"person_orders.person_code as PersonCode",
				"person_orders.person_price AS PersonPrice",
				"person_orders.person_form AS PersonForm",
				"person_orders.person_to AS PersonTo",
			).Joins("LEFT JOIN people ON person_orders.pid=people.id").
			Where("person_orders.id=?", id).Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": data,
	})
}
